//
//  LLKProgressView.m
//  linkingGame
//
//  Created by 李佳 on 15/4/4.
//  Copyright (c) 2015年 LiJia. All rights reserved.
//

#import "ProgressView.h"

#define kDefaultProgressBarHeight 5

@implementation ProgressView

- (id)init
{
    return [self initWithFrame: CGRectZero] ;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame: frame] )
    {
        self.backgroundColor = [UIColor clearColor] ;
        self.innerColor = [UIColor blueColor    ] ;
        self.outerColor = [UIColor redColor] ;
        self.emptyColor = [UIColor clearColor] ;
        self.preferredFrameHeight = frame.size.height - 2;
        self.progress = 1.0f;
    }
    return self ;
}

- (void)setProgress:(float)theProgress
{
    // make sure the user does not try to set the progress outside of the bounds
    if (theProgress > 1.0f)
        theProgress = 1.0f ;
    if (theProgress < 0.0f)
        theProgress = 0.0f ;
    
    _progress = theProgress ;
    [self setNeedsDisplay] ;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext() ;
    
    // save the context
    CGContextSaveGState(context) ;
    
    // allow antialiasing
    CGContextSetAllowsAntialiasing(context, TRUE) ;
    
    // we first draw the outter rounded rectangle
    rect = CGRectInset(rect, 1.0f, 1.0f) ;
    CGFloat radius = 0.5f * rect.size.height ;
    
    [self.outerColor setStroke] ;
    CGContextSetLineWidth(context, 2.0f) ;
    
    CGContextBeginPath(context) ;
    CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMidY(rect)) ;
    CGContextAddArcToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetMidX(rect), CGRectGetMinY(rect), radius) ;
    CGContextAddArcToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMidY(rect), radius) ;
    CGContextAddArcToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMidX(rect), CGRectGetMaxY(rect), radius) ;
    CGContextAddArcToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMidY(rect), radius) ;
    CGContextClosePath(context) ;
    CGContextDrawPath(context, kCGPathStroke) ;
    
    // draw the empty rounded rectangle (shown for the "unfilled" portions of the progress
    rect = CGRectInset(rect, 3.0f, 3.0f) ;
    radius = 0.5f * rect.size.height ;
    
    [self.emptyColor setFill] ;
    
    CGContextBeginPath(context) ;
    CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMidY(rect)) ;
    CGContextAddArcToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetMidX(rect), CGRectGetMinY(rect), radius) ;
    CGContextAddArcToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMidY(rect), radius) ;
    CGContextAddArcToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMidX(rect), CGRectGetMaxY(rect), radius) ;
    CGContextAddArcToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMidY(rect), radius) ;
    CGContextClosePath(context) ;
    CGContextFillPath(context) ;
    
    // draw the inside moving filled rounded rectangle
    radius = 0.5f * rect.size.height ;
    
    // make sure the filled rounded rectangle is not smaller than 2 times the radius
    rect.size.width *= self.progress ;
    if (rect.size.width < 2 * radius)
        rect.size.width = 2 * radius ;
    
    [self.innerColor setFill] ;
    
    CGContextBeginPath(context) ;
    CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMidY(rect)) ;
    CGContextAddArcToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect), CGRectGetMidX(rect), CGRectGetMinY(rect), radius) ;
    CGContextAddArcToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect), CGRectGetMaxX(rect), CGRectGetMidY(rect), radius) ;
    CGContextAddArcToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect), CGRectGetMidX(rect), CGRectGetMaxY(rect), radius) ;
    CGContextAddArcToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect), CGRectGetMinX(rect), CGRectGetMidY(rect), radius) ;
    CGContextClosePath(context) ;
    CGContextFillPath(context) ;
    
    // restore the context
    CGContextRestoreGState(context) ;
}
@end
